package com.hyfrogx.modules.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyfrogx.common.utils.Constant;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.manage.entity.CoindogLiveEntity;
import com.hyfrogx.modules.manage.entity.HyFlashTypeEntity;
import com.hyfrogx.modules.manage.form.flash.FlashTypeForm;
import com.hyfrogx.modules.manage.service.HyFlashLiveService;
import com.hyfrogx.modules.manage.service.HyFlashTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/9/26 10:10
 * @Company 汉咏科技
 * 快讯类型控制类
 */
@RestController
@RequestMapping("/flash/type")
@Api(value = "HyFlashTypeController", tags = {"后台--快讯类型管理"})
public class HyFlashTypeController {

    @Autowired
    private HyFlashTypeService hyFlashTypeService;
    @Autowired
    private HyFlashLiveService hyFlashLiveService;

    /**
     * 获取所有快讯类型
     */
    @PostMapping("/getAll")
    @ApiOperation("获取所有快讯类型")
    public R getAll()
    {
        List<HyFlashTypeEntity> list = hyFlashTypeService.list(new QueryWrapper<HyFlashTypeEntity>().eq("status", FrogConstant.FlashType.RELEASE.getValue()));
        return R.ok().put("List",list);
    }
    /**
     * 分页查询快讯分类
     * @param map 分页参数
     * @return
     */
    @PostMapping("/get")
    @ApiOperation("分页查询快讯分类")
    public R get(@RequestBody Map<String,Object> map)
    {
        IPage<HyFlashTypeEntity> iPage = hyFlashTypeService.getByPage(map);
        return R.ok().put("result",iPage);
    }

    /**
     * 新增快讯分类
     * @param flashTypeForm
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation("新增快讯分类")
    public R insert(@RequestBody @Validated FlashTypeForm flashTypeForm)
    {
        Boolean b = hyFlashTypeService.saveFlash(flashTypeForm);
        if (b)
        {
            return R.ok();
        }
        else
        {
            return R.error("新增失败");
        }
    }

    /**
     * 编辑快讯分类
     * @param flashTypeForm
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("编辑快讯分类")
    public R update(@RequestBody @Validated FlashTypeForm flashTypeForm)
    {
        if (hyFlashTypeService.updateFlash(flashTypeForm))
        {
            return R.ok();
        }
        else
        {
            return R.error("编辑失败");
        }
    }

    /**
     * 更改快讯类型状态
     * @param id
     * @return
     */
    @PostMapping("/update/status/{id}")
    @ApiOperation("更改快讯类型状态")
    public R updateStatus(@PathVariable("id")Integer id)
    {
        //查找该类型下是否拥有未下架快讯
        List<CoindogLiveEntity> list = hyFlashLiveService.list(new QueryWrapper<CoindogLiveEntity>().eq("type_id",id).eq("status",1));
        if (list.size()>0)
        {
            return R.error("快讯类型下拥有未下架快讯");
        }
        if (hyFlashTypeService.updateStatus(id))
        {
            return R.ok();
        }
        else {
            return R.error("状态更改失败");
        }
    }

}
